/**
 * class name: iselect,
 * copyright: maximus zhang,
 * License: MIT-LICENSE,
 * contact: maximus.zhang@gmail.com,
 * required-js: http://jsease.googlecode.com/svn/trunk/core.js, ipop.js,
 * update: http://jsease.googlecode.com/svn/trunk/iselect.js
 * ▼▲ &#9660; &#9650; > &#9658; < &#9668; ✓&#10003;
 */
function iselect(){/* {id, } */
    var A = arguments,
        _ = this;
    _ = ext( _, ext({
        id : 'iselect'+ now(3),
        elem: me.body,
        width:190,/* text input */
        size:12,/* select body show 12 opts, >12 opts will overflow */
        optwidth:180,
        optheight:19,/* select opt height/line-height */
        titlewidth:50,
        name:'iselect',/* form hidden item name */
        textname:'',/* form showed input name */
        textvalue:'text',/* form showed input value type */
        target:'',
        form: null,
        readonly:1,
        fontcase:'',
        /* async:0, */
        seltype:'select',/* select | pop */
        opts: [{text:'', value:'', sel: true}],
        topopt: []
        }, A[0] ) );

    _.uitype = 'iselect';

    _.elem = $(_.elem) || me.body;
    _.tmpopts = [];
    _.target = $(_.target);
    _.form = $(_.form);
    _.showed = _.focused = false;
    if( !/text|title/i.test(_.textvalue) ){
        _.textvalue = 'text';
    }
}
iselect.prototype = {
    hide : function(seled){
        var _ = this;
        if ( 1==seled ) {
            _.showed = _.focused = false;
            _.ipop.hide();
            1==_.bound && unbind(me, 'mousedown', function(){_.hide();});
            _.bound = 0;
        }
        if ( !_.showed && _.focused ) {
            return _;
        }
        if ( !_.showed && !_.focused ) {
            _.ipop.hide();
            1==_.bound && unbind(me, 'mousedown', function(){_.hide();});
            _.bound = 0;
        }

    return _;
    },

    show : function(msg){
        var _ = this;
        if ( 1==_.bound ) {
            return _;
        }
        _.poz = pos(_.$);

        _.ipop.show({css:'position:absolute;top:'+ _.poz._bottom +'px;left:'+ (_.poz.left+1) +'px;border:0;padding:0;width:'+ _.width +'px;-moz-border-radius:0;border-radius:0;z-index:10001;', data:_.body});

        if ( _.isel ) {
            _.iopt.scrollTop = _.isel.offsetTop;
        } else if ( _.val.value.length && _.textval.value.length ) {
            for ( var z=0,L=_.iopt.childNodes.length; z<L; z++ ) {
                var zo = _.iopt.childNodes[z];
                if ( attr(zo,'value')==_.val.value && attr(zo, 'text')==_.textval.value ) {
                    _.isel = zo.addClass('sel');
                    _.iopt.scrollTop = _.isel.offsetTop;
                }
            }
        }

        bind(me, 'mousedown', function(){_.hide();});

        _.bound = 1;
        _.showed = true;
    return _;
    },

    selval : function () {
        var _ = this;
        var value = hop(_, 'value') ? _.value : '',
            text = hop(_, 'text') ? _.text : '';
        _.val.value = value;
        _.textval.value = text;

        if ( _.target.nodeName == 'INPUT' ) {
            _.target.value = value;
        } else if ( _.target.nodeType ) {
            _.target.innerHTML = value;
        }

        if ( typeof _.onchange =='function' ) {
            _.onchange();
        }
    return _.hide(1);
    },

    clearOpts : function() {
        var _ = this;
        _.text = '';
        _.value = '';
        _.tmpopts = [];
        _.selval();
    return _;
    },

    addOpts : function (ops) {
        var _ = this;
        if ( !isArray(ops) ) {
            /* clear all opts if has */
            _.iopt.empty();
            if ( _.isel ) {
                _.isel.remove();
                delete _.isel;
            }
            if ( hop(_, 'value') ) {
                _.value = '';
            }
            if ( hop(_, 'text') ) {
                _.text = '';
            }
            _.selval();

            var ops = _.tmpopts = _.topopt.concat(typeof _.opts == 'function'? _.opts() : _.opts);
        } else {
            if ( typeof _.opts == 'function' ) {
                _.tmpopts = _.topopt.concat(_.tmpopts.concat([_.opts(), ops]));
            } else {
                _.tmpopts = _.topopt.concat(_.tmpopts.concat([_.opts, ops]));
            }
        }

        for ( var k=0, L=ops.length; k<L; k++ ) {
            var optxt = hop(ops[k].text, _.textvalue) ? ops[k].text[_.textvalue] : ops[k].text||'',
                opta = elm('a').setClass('opt').attr('href', '#').attr('value', ops[k].value)
                        .attr('text', optxt).appendTo(_.iopt);
            var optt = elm('div').setClass('txt').appendTo(opta);
            if ( ops[k].sel ) {
                if ( _.isel ) {
                    _.isel.delClass('sel');
                }
                opta.addClass('sel');
                _.isel = opta;
                _.text = optxt;
                _.value = ops[k].value;
                _.textval.attr('value', _.text);
                _.val.attr('value', _.value);
                _.selval();
            }
            if ( typeof ops[k].text =='object' ) {
                var optttit = elm('div').setClass('title').appendTo(optt).html(ops[k].text.title || '');
                optttit.style.width = _.titlewidth +'px';
                var optttxt = elm('div').setClass('text').appendTo(optt).html(ops[k].text.text || '');
            } else {
                optt.html(ops[k].text);
            }

            optt.style.cssText = 'height:'+ _.optheight +'px;line-height:'+ _.optheight +'px;';

            opta.onclick = function () {
                _.value = attr(this, 'value');
                _.text = attr(this, 'text');

                if ( _.isel ) {
                    _.isel.delClass('sel');
                }

                _.isel = this.addClass('sel');

                _.selval();

            return false;
            };
        }

        _.iopt.style.height = _.tmpopts.length>_.size ? _.optheight*_.size +'px' : 'auto';
        _.iopt.style.borderWidth = _.tmpopts.length===0 ? 0 : '1px';

    return _;
    },

    init : function(){
        var _ = this;
        _.$ = elm('div').appendTo(_.elem).setClass('iselect');
        _.id = _.$.id = p(_, 'id') && !$(_.id) ? _.id : ('iselect_'+ now(3));
        _.$.style.width = _.width +'px';


        /* selector */
        _.cap = elm('div').setClass('cap').appendTo(_.$);
        _.tit = elm('div').setClass('tit').appendTo(_.cap);
        _.textval = elm('input').attr('type', 'text').attr('name', _.textname).attr('value', '').setClass('textval').appendTo(_.tit);
        _.fontcase && _.textval.addClass(_.fontcase);
        _.readonly && _.textval.attr('readonly', 'readonly');

        _.val = elm('input').attr('type', 'hidden').attr('name', _.name).attr('value', '').appendTo(_.tit);

        _.sel = elm('div').setClass('sel').appendTo(_.cap);
        _.msa = elm('a').setClass('msa').attr('href', '#').appendTo(_.sel);
        _.selA = elm('div').setClass('selA').appendTo(_.msa).html('&#9650;');
        _.selV = elm('div').setClass('selA selV').appendTo(_.msa).html('&#9660;');

        if ( !_.form || _.form.nodeName != 'FORM' ) {
            _.form = _.elem;
            while ( _.form.nodeName != 'FORM' ) {
                _.form = _.form.parentNode;
                if ( _.form == me.body ) {
                    break;
                }
            }
        }
        if ( _.form.nodeName =='FORM' ) {
            bind(_.form, 'reset', function(){_.clearOpts().addOpts();});
        }

        _.body = elm('div').setClass('iselect').appendTo(me.body);
        _.body.style.width = (_.optwidth>_.width?_.optwidth:_.width) +'px';

        _.iopt = elm('div').setClass('opts').appendTo(_.body);

        /* ipop.js required */
        _.ipop = (new ipop( { id: _.id +'_ipop', nomask: 1, notitle: 1, radius:0, autocenter:0, z:10001, data: _.body } )).init();

        _.ipop.$.onmouseover = function () {
            _.focused = true;
        return true;
        };
        _.ipop.$.onmouseout = function () {
            _.focused = false;
        return true;
        };

        _.textval.onfocus = _.textval.onclick = function(){
            _.show();
        };

        _.textval.onblur = function () {
            _.showed = false;
        return _.hide();
        };

        _.sel.onmouseover = function () {
            _.focused = true;
        return _;
        };

        _.sel.onmouseout = function () {
            _.focused = false;
        return _;
        };

        if(/pop/i.test(_.seltype)) {
            _.selpop = window.iselect_pop || def('iselect_pop', 'ipop', { id: 'iselect_pop', ico:{src:'/icos/magnifier-left.png'}, z:8000 } );
            _.msa.onclick=function() {
                if(_.isel){
                    _.isel.delClass('sel');
                    delete _.isel;
                }
                _.ipop.hide();
                _.selpop.show( {data:$(_.pop)} );
            return false;
            };
        } else {
            _.msa.onclick=function () {
                _.show();
                _.showed = false;
            return false;
            };
        }

        _.addOpts();

    return _;
    }

};